
import Vue from 'vue';  
import Router from "vue-router";
import routes from './routes';
import iView from 'iview';
import {Storage} from 'commons/js/utils';

Vue.use(Router); 
let router = new Router({routes});

//  判断是否需要登录权限 以及是否登录
router.beforeEach((to, from, next) => {
    iView.LoadingBar.start(); 
    if (to.matched.some(res => res.meta.requiresAuth)) {
        if (Storage.getItem('B_TOKEN')) { // 判断是否登录
            next();
        } else {
            alert('身份验证已过期，请重新登录');
            if(from.name == 'login') {
                iView.LoadingBar.finish();
                next(false);
            }
            else next('/login');
        }
    } else {
        next();
    }
});

// 路由文件渲染完成
router.beforeResolve((to, from,next) => {
  iView.LoadingBar.finish();
  next();
})

export default router; 